package com.tom.demo.common.component;
import cn.dev33.satoken.stp.StpInterface;
import com.tom.demo.mapper.SysMenuMapper;
import com.tom.demo.mapper.SysRoleMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
 * 自定义权限加载接口实现类
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysMenuMapper sysMenuMapper;



    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        if("1".equals(loginId.toString())){
            Set<String> roleKeys = sysRoleMapper.findByRoleKey(null);
            return new ArrayList<>(roleKeys);
        }else {
            Set<String> roleKeys = sysRoleMapper.findByRoleKey(loginId.toString());
            return new ArrayList<>(roleKeys);
        }
    }

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        if("1".equals(loginId.toString())){
            Set<String> roleKeys = sysMenuMapper.findByPerms(null);
            return new ArrayList<>(roleKeys);
        }else {
            Set<String> roleKeys = sysMenuMapper.findByPerms(loginId.toString());
            return new ArrayList<>(roleKeys);
        }
    }

}
